package com.ruoyi.quartz.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.hospital.domain.HPatient;
import com.ruoyi.hospital.domain.HPatientPdf;
import com.ruoyi.hospital.domain.ViewCpjsPatlist;
import com.ruoyi.hospital.service.IHpatientPdfService;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.hospital.service.IViewCpjsPatlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 同步患者任务
 * @author liz
 * @date 2023-10-21 14:25
 */
@Component("syncPatientTask")
public class SyncPatientTask {

    @Autowired
    IViewCpjsPatlistService viewCpjsPatlistService;

    @Autowired
    IHpatientService hpatientService;

    @Autowired
    IHpatientPdfService hpatientPdfService;

    public void syncPatient() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<ViewCpjsPatlist> list = viewCpjsPatlistService.list(new QueryWrapper<ViewCpjsPatlist>()
                .select("name", "inpatient_no", "admiss_times", "ward_name", "effective_e",
                "bed_no", "resp_name", "patient_id", "dis_ward", "dis_dept", "settle_name", "settle_type","jsd_pdf","emr_web"));
        DynamicDataSourceContextHolder.clearDataSourceType();
        list.forEach(item -> {
            HPatient hPatient = hpatientService.getOne(new QueryWrapper<HPatient>().eq("inpatient_no", item.getInpatientNo()).eq("admiss_times",item.getAdmissTimes()));
            if (ObjectUtil.isEmpty(hPatient)) {
                // 保存患者
                HPatient enty = new HPatient();
                BeanUtil.copyProperties(item, enty);
                enty.setCreateTime(new Date());
                enty.setUpdateTime(new Date());
                if(item.getJsdPdf() != null){
                    // 保存患者pdf
                    HPatientPdf hPatientPdf = new HPatientPdf();
                    hPatientPdf.setJsdPdf(item.getJsdPdf());
                    hpatientPdfService.saveOrUpdate(hPatientPdf);
                    // 更新患者pdfid
                    enty.setJsdPdfId(Convert.toStr(hPatientPdf.getId()));
                }
                hpatientService.saveOrUpdate(enty);
            } else {
                HPatient enty = new HPatient();
                BeanUtil.copyProperties(item, enty);
                enty.setCreateTime(new Date());
                enty.setId(hPatient.getId());
                if(item.getJsdPdf() != null){
                    if(hPatient.getJsdPdfId() != null){
                        HPatientPdf hPatientPdf = new HPatientPdf();
                        hPatientPdf.setJsdPdf(item.getJsdPdf());
                        hPatientPdf.setId(Integer.parseInt(hPatient.getJsdPdfId()));
                        hpatientPdfService.updateById(hPatientPdf);
                    } else {
                        // 保存患者pdf
                        HPatientPdf hPatientPdf = new HPatientPdf();
                        hPatientPdf.setJsdPdf(item.getJsdPdf());
                        hpatientPdfService.save(hPatientPdf);
                        // 更新患者pdfid
                        enty.setJsdPdfId(Convert.toStr(hPatientPdf.getId()));
                    }
                }
                hpatientService.updateById(enty);
            }
        });
    }
}
